<?php

namespace Logic\User;

use Core\Exception;
use Helper\StringHelper;
use think\Db;


class UserLogic
{
    private $mUser;

    function __construct()
    {
        $this->mUser = new \UserModel();
    }

    /**
     * 获取用户信息
     * @param string $uid
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserInfo($userId = '')
    {
        $userInfo = $this->mUser->where('id', $userId)->find();
        if ($userInfo['yst_user_type'] != 2) {
            $result = [
                'realname' => $userInfo['realname'],
                'cardType' => $userInfo['cardType'],
                'idcard' => $userInfo['idcard'],
                'mobile' => $userInfo['mobilePhone'],
                'isLegal' => $userInfo['isLegal'],
                'yst_user_type' => $userInfo['yst_user_type'],
                'special_status' => $userInfo['special_status'],
                'yst_contract_status' => $userInfo['yst_contract_status'],
                'headImg' => $userInfo['headImg']
            ];
            $store = Db::name('shop')->field('province_id,city_id')->where(['user_id' => $userId, 'is_logoff' => 0])->find() ?: [];
            if (!empty($upgrade)) {
                $result = array_merge($result, $store);
            }

        } else {
            $result = Db::name('user')->alias('t1')
                ->leftJoin('t_user_enterprise t2', 't2.user_id = t1.id')
                ->leftJoin('t_area t3', 't2.province = t3.id')
                ->leftJoin('t_area t4', 't2.city = t4.id')
                ->field("t1.id,t1.usableSum,concat('" . IMG_URL . "',t1.headImg) as headImg,t1.yst_user_type,t1.yst_contract_status,t2.company_name,t2.company_address,t2.company_phone,t2.agent as realname ,t1.cardType,t2.agent_idcard as idcard,t1.mobilePhone as mobile,t1.personCode,t2.parentBankName,t3.name as province,t4.name as city,t2.bankName,t2.accountNo")
                ->where('t1.id', $userInfo['id'])
                ->find();
            // 敏感字段脱敏
            $result['personCode'] = StringHelper::convertBusCard($result['personCode']);
            $result['accountNo'] = StringHelper::convertBankCard($result['accountNo']);
            $result['bankName'] = StringHelper::convertZhStr($result['bankName']);
        }

        //微信关联状态
        $unionid = Db::name('user')->where(['id' => $userId, 'is_logoff' => 0])->value('unionid');
        $result['is_bind_wechat'] = empty($unionid) ? 0 : 1;
        return $result;
    }
}